<?php
/**
 * 业主首页
 * @author Administrator
 *
 */
class index extends CControllerOwnerBase{
	
	public function show()
	{
		$newsModel = new newsModel();
		$newsList = $newsModel->getTopForIndex();
		
		/*if(isset($_SESSION["newShop"]))
		{
			$newSP = $_SESSION["newShop"];
			$topSP = $_SESSION["topShop"];
		}
		else{*/
			$spModel = new shopProductModel();
			$newSP = $spModel->getNewShop($this->_cityName);
			$topSP = $spModel->getTopShop($this->_cityName);
			//var_dump($topSP);
			/*$_SESSION["newShop"] = $newSP;
			$_SESSION["topShop"] = $topSP;
		}*/
		$topHtml = "<table><tr>";
		$i=0;
		foreach ($topSP as $item)
		{
			$i++;
			$topHtml .= '
					<td>
					<table style="width:300px;">
						<tr>
							<td align="center" style="width:290px; height:199px;"><a href="/shoplist/detail?id='.$item["shop_id"].'"><img id="'.$item["shop_id"].'" imgsrc="'.$item["pic_path"].'" /></a></td>
						</tr>
						<tr>
							<td><div><span class="color_b">['.$item["shop_simple_name"].']</span><a href="/shoplist/detail?id='.$item["shop_id"].'">'.$item["p_name"].'</a></div></td>
						</tr>
					</table>
					</td>';
			
			if($i>0 && $i%2==0)
			{
				$topHtml .= "</tr><tr>";
			}
		}
		$topHtml .= "</tr></table>";
		
		if(CCommon::isIE())
		{
			$this->setViewParam("isIE",1);
			$this->setJsBlock("initUserPwd();");
		}
		$this->setJsBlock("shopImpThumb('o_index_list');");
		$this->render("index",array("showAn"=>1,"newsList"=>$newsList,"newSPList"=>$newSP,"topHtml"=>$topHtml));
	}
	
	public function code() {
		$c = new CCommon();
		
		$c->code("ownerCode");
	}
	
	public function userLogin(){
		$checkcode = $this->getRequestData('txt_checkcode');
		if (strtolower($_SESSION['ownerCode']) !== strtolower($checkcode)) {
			echo -1;
			exit;
		}
		
		$username = $this->getRequestData("user_name");
		$pwd = $this->getRequestData("pwd");
		$pwd = CCommon::pwdMd5($pwd);

		$model = new ownerModel();
		$loginInfo = $model->userLogin($username, $pwd);

		if($loginInfo)
		{
			$first_login = $loginInfo["first_login"];
			unset($loginInfo["first_login"]);
			$_SESSION["ownerInfo"] = $loginInfo;
			if($first_login==1)
			{
				echo 1;
			}else{
				echo 2;
			}
		}
		else{
			echo -2;
		}
		exit;
	}
	
	public function logout()
	{
		parent::logout();
	}

	public function pwd_forget()
	{
		$rule = CCustomFormValidator::forgetPwdValidator();
		$this->formValidator->init(array("formID"=>"formForget", "alertErrorMsg" => false));
		$this->formValidator->setRules($rule);
		$msg = "";
		if ($_POST) {
			$error = $this->formValidator->run();
			if ($error) {
				echo "error";
				exit;
			}
			
			$username = $this->getRequestData("txt_username");
			$email = $this->getRequestData("txt_mail");
			
			$model = new ownerModel();
			
			$ownerID = $model->checkForgetInfo($username,$email);
			if($ownerID)
			{
				$newPwd = CCommon::generateRandPwd(6);
				$r = CCommon::sendToMail($email,"一卡通密码找回","尊敬的用户您好，您的新密码是：".$newPwd.",请登录后及时修改。");
				if($r)
				{
					$modelInfo["owner_id"] = $ownerID;
					$modelInfo["user_pwd"] = CCommon::pwdMd5($newPwd);

					$model->updateByPkId($modelInfo);

					$this->redirectUrl("/index/show_result");
				}
				else{
					$msg = "邮件发送失败，请联系管理员重置密码。";
				}
			}
			else{
				$msg = "用户名或邮箱填写错误!";
			}
		}
		
		$this->setStyleFile("/css/validator.css");
		$this->setJsFile(array("/js/formValidator.js","/js/formValidatorRegex.js"));
		$this->render("pwd_forget",array("msg"=>$msg));
	}
	
	public function show_result()
	{
		$this->render("show_result");
	}
}